15. 合并数据集
合并数据集
1. 重命名 2008 列,以在合并后与 2018 列进行区分
为此,请使用 Pandas 的 rename() 和 lambda 函数。查看
这里
的示例 。在 lambda 函数中,取列标签的前 10 个字符,并将其与
_2008
连接起来。(仅取前 10 个字符,以避免列名过长。)
lambda 函数看起来应该是这样的:
lambda x: x[:10] + "_2008"
在你的
rename
中,不要忘了在添加 lambda 函数时指定参数
columns=
!
2. 执行内部合并
要回答最后一个问题,我们只想知道同型号的汽车有何更新,以及新车型的 mpg 与旧型号的 mpg 相比如何。
执行内部合并,左侧为
model_2008
和右侧为
model
。
在此
查看 Pandas 的 merge 函数文档 。
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter
- Opened files (when workspace is loaded): n/a